//
//  ShowGoodsViewController.m
//  XiJianChui
//
//  Created by aaa on 16/7/18.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "ShowGoodsViewController.h"
#import <SDCycleScrollView.h>
#import "QhGoodClassificationCell.h"
#import "showGoodCollectionViewCell.h"
#import "recommendCollectionReusableView.h"
static NSString * const showGoodID = @"showGoodCollectionViewCell";
static NSString * const recommendID = @"recommend";
static NSInteger const cols = 2;
static CGFloat const margin = 10;
static NSString * const goodClassificationCellID = @"goodClassificationCell";
#define HeightForBanner QhScreenH * 0.3
#define HeightForCollectionViewHeaderView 55
#define itemWH (QhScreenW - (cols - 1) * margin) / cols
@interface ShowGoodsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
//广告轮播
@property (nonatomic, weak) SDCycleScrollView *banner;
//推荐商品
@property (nonatomic, weak) UICollectionView *recommendGoodsView;
//图片数组
@property (nonatomic, strong) NSArray *picturesArray;
@end

@implementation ShowGoodsViewController
- (NSArray *)picturesArray
{
    if (!_picturesArray) {
        _picturesArray = [NSArray arrayWithObjects:@"fsh_ad1",@"fsh_ad2",@"fsh_ad3",@"fsh_ad4", nil];
    }
    return _picturesArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"商品展示";
    self.tableView.backgroundColor = QhCommonColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.sectionFooterHeight = QhMarin;
    
    self.tableView.rowHeight = 100;

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([QhGoodClassificationCell class]) bundle:nil] forCellReuseIdentifier:goodClassificationCellID];
    
    [self createBanner];
    
    [self createRecommendGoods];
}
#pragma mark - create UI
- (void)createBanner
{
    CGRect bannerFrame = CGRectMake(0, 0, QhScreenW, HeightForBanner);
    _banner = [SDCycleScrollView cycleScrollViewWithFrame:bannerFrame imageNamesGroup:self.picturesArray];
    self.tableView.tableHeaderView = _banner;
}

- (void)createRecommendGoods
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell尺寸
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
    layout.headerReferenceSize = CGSizeMake(QhScreenW, HeightForCollectionViewHeaderView);
    // 创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 800) collectionViewLayout:layout];
    _recommendGoodsView = collectionView;
    collectionView.backgroundColor = self.tableView.backgroundColor;
    
    self.tableView.tableFooterView = collectionView;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.scrollEnabled = NO;
    
    // 注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"showGoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:showGoodID];
    
    [collectionView registerNib:[UINib nibWithNibName:@"recommendCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendID];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QhGoodClassificationCell *cell = [tableView dequeueReusableCellWithIdentifier:goodClassificationCellID];
    
    return cell;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    showGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:showGoodID forIndexPath:indexPath];
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    recommendCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendID forIndexPath:indexPath];
    return headerView;
}
@end
